import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.prefs.BackingStoreException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StarterAssignment3 {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyFileReader fileReader = new MyFileReader("2002-05-01.xml");
		
		Pattern beginPackagePattern = Pattern.compile(".*<Model:Package .*", Pattern.DOTALL);
		Pattern endPackagePattern = Pattern.compile(".*</Model:Package>.*", Pattern.DOTALL);
		Pattern classIDPattern = Pattern.compile(".*<Model:Class xmi.id = '([^']*)'.*", Pattern.DOTALL);
		Pattern namePattern = Pattern.compile(".*name = '([^']*)'.*", Pattern.DOTALL);
		Pattern anotationPattern = Pattern.compile(".*annotation = '([^']*)'.*", Pattern.DOTALL);
		Pattern supertypesPattern = Pattern.compile(".*supertypes = '([^']*)'.*", Pattern.DOTALL);
		Pattern endClassPattern1 = Pattern.compile(".*/>.*", Pattern.DOTALL);
		Pattern endClassPattern2 = Pattern.compile(".*</Model:Class>.*", Pattern.DOTALL);
		
		BufferedReader reader = new BufferedReader(new StringReader(fileReader.read().toString()));
		HashMap<String, HashMap<String, String>> classAttributeCount = new HashMap<String, HashMap<String, String>>();
		ArrayList<String> packageNames = new ArrayList<String>();
		boolean classBegan = false;
		boolean packageBegan = false;
		String classID = null;
		String curPackage = null;
		String line = null;
		Matcher m ;
		
		try {
			while ((line = reader.readLine()) != null) {
				//GET PACKAGE NAME
				m = beginPackagePattern.matcher(line);
				if (m.matches()){
					System.out.println(line);
					packageBegan = true;
				}
				
				m = namePattern.matcher(line);
				if (packageBegan && curPackage == null && m.matches()){
					System.out.println(line+" group = "+m.group(1));
					packageNames.add(m.group(1));
					curPackage = m.group(1);
				}
				
				m = endPackagePattern.matcher(line);
				if (m.matches()){
					System.out.println("END"+line);
					packageBegan = false;
					curPackage = null;
				}
				
				//GET CLASS ATTRIBUTES
				if (classBegan){
					m = namePattern.matcher(line);
					if (m.matches() && classAttributeCount.get(classID).get("name") == null){
						System.out.println(line+" group = "+m.group(1));
						classAttributeCount.get(classID).put("name", m.group(1));
					}
					
					m = anotationPattern.matcher(line);
					if (m.matches() && classAttributeCount.get(classID).get("annotation") == null){
						classAttributeCount.get(classID).put("annotation", m.group(1));
						System.out.println(line+" group = "+m.group(1));
					}
					
					m = supertypesPattern.matcher(line);
					if (m.matches()){
						classAttributeCount.get(classID).put("supertypes", m.group(1));
						System.out.println(line+" group = "+m.group(1));
						
						m = endClassPattern1.matcher(line);
						if (m.matches()){
							classBegan = false;
							classID = null;
							System.out.println("END"+line);
						}
					}
					
					m = endClassPattern2.matcher(line);
					if (m.matches()){
						classBegan = false;
						classID = null;
						System.out.println("END"+line);
					}
					
				}
				else{					
					m = classIDPattern.matcher(line);
					if (m.matches()){
						classID = m.group(1);
						classAttributeCount.put(classID, new HashMap<String, String>());
						classBegan = true;
						if (curPackage == null)
							System.out.println("null");
						classAttributeCount.get(classID).put("package", curPackage);
						System.out.println(line+" group = "+m.group(1));
					}
				}
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		HTMLWriter writer = new HTMLWriter();
		writer.writePackagesRoot(packageNames);
		writer.writeClasses(classAttributeCount);
		
	}
}
